package bestfriends.tamu;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.database.CursorJoiner.Result;
import android.os.Environment;

import com.thoughtworks.xstream.XStream;
 
public class ReadXMLFile {

	public static ArrayList<Person> xStreamData(Context context) {
		ArrayList<Person> ret;
		ret = xStreamSDPersonXML();
		if(ret == null) {
			ret = xStreamAssetsPersonXML(context);
		}
		return ret;			
	}
 
	public static ArrayList<Person> xStreamSDPersonXML(){		
 
	  try {
  		File sdcard = Environment.getExternalStorageDirectory();
		File peopleXML = new File(sdcard,"data.xml");
		
		XStream xStream = new XStream();
	    xStream.alias("person", Person.class);
	    xStream.alias("persons", PersonWrapper.class);
	    xStream.addImplicitCollection(PersonWrapper.class,"list");
       
       PersonWrapper people = (PersonWrapper) xStream.fromXML(peopleXML);
       return (ArrayList<Person>) people.getList();
        
	  } catch (Exception e) {
		  e.printStackTrace();
	  }
	  return null;
  }
	
	public static ArrayList<Person> xStreamAssetsPersonXML(Context context){	
			try{
				InputStream inputStream = context.getAssets().open("data.xml");
				BufferedReader f = new BufferedReader(new InputStreamReader(inputStream));
				String xml = "";
				String line;
				while ((line = f.readLine()) != null) {            	  
					xml += line + '\n';
				}
				XStream xStream = new XStream();
			    xStream.alias("person", Person.class);
			    xStream.alias("persons", PersonWrapper.class);
			    xStream.addImplicitCollection(PersonWrapper.class,"list");
			       
			    PersonWrapper people = (PersonWrapper) xStream.fromXML(xml);
			    return (ArrayList<Person>) people.getList();				
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
	}
	
	public static ArrayList<Question> xStreamQuestion(Context context) {
		ArrayList<Question> ret;
		ret = xStreamSDQuestionXML();
		if(ret == null) {
			ret = xStreamAssetsQuestionXML(context);
		}
		return ret;			
	}
 
	public static ArrayList<Question> xStreamSDQuestionXML(){	
 
	  try {
  		File sdcard = Environment.getExternalStorageDirectory();
		File questionsXML = new File(sdcard,"questions.xml");
		
        XStream xStream = new XStream();
        xStream.alias("question", Question.class);
        xStream.alias("result", bestfriends.tamu.Result.class);
        xStream.alias("questions", QuestionWrapper.class);
        xStream.alias("results", ResultWrapper.class);
        xStream.addImplicitCollection(Question.class,"result");
        xStream.addImplicitCollection(QuestionWrapper.class,"list");
        xStream.addImplicitCollection(ResultWrapper.class,"list");
       
      QuestionWrapper questions = (QuestionWrapper) xStream.fromXML(questionsXML);
       return (ArrayList<Question>) questions.getList();
        
	  } catch (Exception e) {
		  e.printStackTrace();
	  }
	  return null;
  }
	
	public static ArrayList<Question> xStreamAssetsQuestionXML(Context context){	
			try{
				InputStream inputStream = context.getAssets().open("questions.xml");
				BufferedReader f = new BufferedReader(new InputStreamReader(inputStream));
				String xml = "";
				String line;
				while ((line = f.readLine()) != null) {            	  
					xml += line + '\n';
				}
				XStream xStream = new XStream();
		        xStream.alias("question", Question.class);
		        xStream.alias("result", bestfriends.tamu.Result.class);
		        xStream.alias("questions", QuestionWrapper.class);
		        //xStream.alias("results", ResultWrapper.class);
		        xStream.addImplicitCollection(Question.class,"result");
		        xStream.addImplicitCollection(QuestionWrapper.class,"list");
		        //xStream.addImplicitCollection(ResultWrapper.class,"list");
			       
			    QuestionWrapper questions = (QuestionWrapper) xStream.fromXML(xml);
			    return (ArrayList<Question>) questions.getList();		
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
	}
}
	
	
	